package com.steven.im.webSocket;

import android.util.Log;

import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketAdapter;
import com.neovisionaries.ws.client.WebSocketException;
import com.neovisionaries.ws.client.WebSocketFrame;
import com.steven.im.base.Constant;

import java.util.List;
import java.util.Map;

import static com.steven.im.webSocket.WSBaseEntity.WSBase.MAX_NUM;

public class WebSocketListener extends WebSocketAdapter {
    private static int maxNum=0;//默认是0
    @Override
    public void onTextMessage(WebSocket websocket, String text) throws Exception {
        super.onTextMessage(websocket, text);
        //接收到消息
        Log.e(Constant.TAG, "onTextMessage:接收到消息："+text );
    }

    @Override
    public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
        super.onConnected(websocket, headers);
        //连接成功
        Log.e(Constant.TAG, "onConnected: 连接成功！" );
    }

    @Override
    public void onConnectError(WebSocket websocket, WebSocketException exception) throws Exception {
        super.onConnectError(websocket, exception);
        //连接失败  如果连接失败  再进行连接
        Log.e(Constant.TAG, "onConnectError: 连接失败！" +exception);
       maxNum++;
        if (maxNum<= MAX_NUM){
            WebSocketClient.getInstance().connectAsynchronously();
        }else {
            maxNum=1;
        }
    }

    @Override
    public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
        super.onDisconnected(websocket, serverCloseFrame, clientCloseFrame, closedByServer);
        //断开连接
        Log.e(Constant.TAG, "onDisconnected: 断开连接！");
    }
}
